package com.hepinghospital.user.ui.widget;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.hepinghospital.user.R;

import jz.joyoung.robot.util.DpUtil;

import static android.graphics.Paint.Style.STROKE;

public class CircleLoadingView extends androidx.appcompat.widget.AppCompatImageView {
    private Paint paint;
    private Context context;
    private float maxTime = 60f;
    private float time;
    private  CircleLoadingTimeOut circleLoadingTimeOut;

    public CircleLoadingView(Context context) {
        super(context);
        this.context = context;
        startAnim();
    }

    public CircleLoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        this.context = context;
        startAnim();
    }

    public CircleLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int circlePoint = getWidth() / 2;
        //第一步:画背景(即内层圆)
        paint.setColor(Color.parseColor("#ECECEC")); //设置圆的颜色
        paint.setStyle(STROKE); //设置空心
        paint.setStrokeWidth(DpUtil.dip2px(context, 5)); //设置圆的宽度
        paint.setAntiAlias(true);  //消除锯齿
        canvas.drawCircle(circlePoint, circlePoint, circlePoint - DpUtil.dip2px(context, 5) / 2, paint); //画出圆

        paint.setColor(getResources().getColor( R.color.mainBlue));  //设置进度的颜色
        RectF oval = new RectF(DpUtil.dip2px(context, 5) / 2, DpUtil.dip2px(context, 5) / 2, getWidth() - DpUtil.dip2px(context, 5) / 2, getWidth() - DpUtil.dip2px(context, 5) / 2);  //用于定义的圆弧的形状和大小的界限
        canvas.drawArc(oval, -90f, 360f * (time / maxTime), false, paint);
    }

    private void startAnim() {
        ValueAnimator animator = ObjectAnimator.ofFloat( 0, maxTime );
        animator.addUpdateListener( new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                time = (float) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        animator.addListener( new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (circleLoadingTimeOut != null) {
                    circleLoadingTimeOut.onTimeOut();
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.setDuration(60000);
        animator.setInterpolator(new LinearInterpolator());   //动画匀速
        animator.start();
    }

    /**
     * 录音完成后的回调
     */
    public interface CircleLoadingTimeOut {
        //时长  和 文件
        void onTimeOut();
    }
}
